<template>
	<view class="comment-all">
		<view class="comment-all-top">
			{{commentData.total}}条评论
		</view>
		<view class="border">
			<view class="content-list">
				<commentTpl ref='commentTpl' :list='commentData.list' :baseUrl='baseUrl' :isLastPage='commentData.isLastPage'
				 :pageAll='false' @commentOne='insertLevelOneComment' @commentTwo='insertLevelTwoComment' :hasLogin='hasLogin'
				 @getMore='getComment' @report='insertReport' @del='delectLevelTwoCommentById' @toThumb='increaseThumb'></commentTpl>
			</view>
		</view>
		<view class="comment-all-bottom">
			{{commentData.isLastPage?'已显示全部评论':'加载更多'}}
		</view>
	</view>
</template>

<script>
	import {
		mapState
	} from 'vuex';
	import commentTpl from '@/components/comment-tpl/comment-tpl.vue'
	import moment from 'moment';
	export default {
		components: {
			commentTpl
		},
		computed: {
			...mapState('userStore', ['hasLogin', 'userInfo'])
		},
		data() {
			return {
				baseUrl: this.$baseUrl,
				commentData: {
					list: []
				},
				commentParams: {
					pageNumber: 1,
					projectId: '',
					type: '',
					pageSize: 10
				},
			}
		},
		onLoad(option) {
			this.commentParams.projectId = option.id;
			this.commentParams.type = option.type
			this.getComment()
		},
		methods: {
			//获取评论信息
			getComment() {
				this.$request.get({
					url: 'LevelOneComment/selectLevelOneComment',
					data: this.commentParams
				}).then(res => {
					for (let k of res.list) {
						k.createTime = this.isToday(k.createTime);

						for (let kk of k.levelTwoComments) {
							kk.createTime = this.isToday(kk.createTime);
						}
					}
					res.list = [...this.commentData.list, ...res.list]
					this.commentData = res
					this.commentParams.pageNumber += 1;
				})
			},
			//评论日期处理
			isToday(time) {
				if (new Date() - new Date(time) < 86400000) {
					return moment(time).format('HH:mm')
				} else {
					return moment(time).format('MM/DD HH:mm')
				}
			},
			//一级评论
			insertLevelOneComment(v) {
				this.$request.post({
					url: 'LevelOneComment/insertLevelOneComment',
					data: {
						linkProjectId: this.id,
						commentContent: v,
						type: this.commentParams.type
					}
				}).then(res => {
					res.linkUserAccountHead = this.userInfo.accountHead;
					res.linkUserName = this.userInfo.realName;
					res.vipLevelIcon = this.userInfo.vipLevelIcon;
					res.vipLevel = this.userInfo.vipLevel;
					res.tags.count = 0;
					res.createTime = moment(new Date().getTime()).format('HH:mm');
					this.commentData.list = [...[res], ...this.commentData.list]
				})
			},
			//二级评论
			insertLevelTwoComment(v) {
				console.log(v)
				this.$request.post({
					url: 'LevelTwoComment/insertLevelTwoComment',
					data: {
						linkCommentId: v.data.levelOneCommentId ? v.data.levelOneCommentId : v.data.linkCommentId,
						whoCommentId: v.data.linkUserId,
						linkTwoCommentId: v.data.levelTwoCommentId,
						hisName: v.data.linkUserName ? v.data.linkUserName : v.data.myName,
						commentContent: v.content,
						type: this.commentParams.type
					}
				}).then(res => {
					res.linkUserAccountHead = this.userInfo.accountHead;
					res.myName = this.userInfo.realName;
					res.vipLevelIcon = this.userInfo.vipLevelIcon;
					res.vipLevel = this.userInfo.vipLevel;
					res.tags.count = 0;
					res.createTime = moment(new Date().getTime()).format('HH:mm');
					const temp = this.commentData.list.find((item) => item.levelOneCommentId === res.linkCommentId);
					temp.levelTwoComments = [...temp.levelTwoComments, ...[res]]
				})
			},
			//举报
			insertReport(v) {
				this.$request.post({
					url: 'Report/insertReport',
					data: {
						linkCommentId: v.data.levelOneCommentId ? v.data.levelOneCommentId : v.data.levelTwoCommentId,
						rank: v.data.levelOneCommentId ? 1 : 2,
						reportContent: v.content,
						type: this.commentParams.type,
					}
				}).then(res => {
					uni.showToast({
						title: '举报成功',
					})
					this.$refs.commentTpl.closePuptTip()
				})
			},

			//删除评论
			delectLevelTwoCommentById(v) {
				console.log(v)
				let that = this;
				uni.showModal({
					title: '提示',
					content: '确认删除当前评论吗?',
					success: function(res) {
						if (res.confirm) {
							let temp = {}
							let url = 'LevelOneComment/delectLevelOneCommentByid'
							if (v.levelOneCommentId) {
								temp.levelOneCommentId = v.levelOneCommentId
								url = 'LevelOneComment/delectLevelOneCommentByid'
							} else {
								temp.LevelTwoCommentId = v.levelTwoCommentId
								url = 'LevelTwoComment/delectLevelTwoCommentById'
							}

							that.$request.get({
								url: url,
								data: temp
							}).then(res => {
								let data = ''
								if (v.levelOneCommentId) {
									data = that.commentData.list.findIndex((item) => {
										return item.levelOneCommentId === v.levelOneCommentId
									});
									that.commentData.list.splice(data, 1);
								} else {
									for (let k of that.commentData.list) {
										data = k.levelTwoComments.findIndex((item) => {
											return item.levelTwoCommentId === v.levelTwoCommentId
										});
										k.levelTwoComments.splice(data, 1);
									}

								}

								uni.showToast({
									title: '删除成功',
								})
							})

							that.$refs.commentTpl.closePup()
						} else if (res.cancel) {
							that.$refs.commentTpl.closePup()
						}
					}
				});

			},
			// 点赞
			increaseThumb(v) {
				if (v.tags.flag == 1) {
					this.$request.post({
						url: 'Tags/cancelTag',
						data: {
							tagsId: v.tags.tagsId
						}
					}).then(res => {
						let data = {}
						if (v.levelOneCommentId) {
							data = this.commentData.list.find((item) => item.levelOneCommentId === v.levelOneCommentId);
						} else {
							for (let k of this.commentData.list) {
								for (let kk of k.levelTwoComments) {
									if (kk.levelTwoCommentId == v.levelTwoCommentId) {
										data = kk
									}
								}
							}
						}
						data.tags.count = res
						data.tags.flag = 0
					})
				} else {
					this.$request.post({
						url: 'Tags/increaseThumb',
						data: {
							tagsId: v.tags.tagsId
						}
					}).then(res => {
						let data = {}
						if (v.levelOneCommentId) {
							data = this.commentData.list.find((item) => item.levelOneCommentId === v.levelOneCommentId);
						} else {
							for (let k of this.commentData.list) {
								for (let kk of k.levelTwoComments) {
									if (kk.levelTwoCommentId == v.levelTwoCommentId) {
										data = kk
									}
								}
							}
						}
						data.tags.count = res
						data.tags.flag = 1
					})
				}

			}
		},
		onReachBottom() {
			if (!this.commentData.isLastPage) {
				this.getComment()
			}
		}
	}
</script>

<style lang="less">
	page {
		background-color: #F5F5F5;
		height: 100%;
		width: 100%;
	}

	.comment-all {
		.comment-all-top {
			height: 78upx;
			color: #757575;
			font-size: 26upx;
			padding-left: 32upx;
			line-height: 78upx;
		}
		
		.border{
			background-color: #FFFFFF;
			.content-list{
				padding: 20upx;
				box-sizing: border-box;
			}
		}

		.comment-all-bottom {
			text-align: center;
			padding: 48upx;
			color: #A3A3A3;
			font-size: 24upx;
		}
	}
</style>
